import Vue from 'vue';
import server from 'axios';
import Qs from 'qs';
import {
    LoadingPlugin,
    ToastPlugin
} from 'vux'
Vue.use(LoadingPlugin)
Vue.use(ToastPlugin)

var axios = server.create({
    // baseURL: 'https://some-domain.com/api/',
    timeout: 6000,
    transformRequest: [function (data) {
        return Qs.stringify(data);
    }],
  });

// 添加请求拦截器
axios.interceptors.request.use(function (config) {
    Vue.$vux.loading.show({
        text: 'Loading'
    })
    return config;
}, function (error) {
    return Promise.reject(error);
});

// 添加响应拦截器
axios.interceptors.response.use(function (response) {
    Vue.$vux.loading.hide()
    if (response.data.code == 200 || response.data.code == 10000) {
        return response;
    } else {
        Vue.$vux.toast.show({
            type: 'cancel',
            text: '网络错误，请稍后再试'
        })
    }
}, function (error) {
    Vue.$vux.loading.hide()
    Vue.$vux.toast.show({
        type: 'cancel',
        text: '请求超时'
    })
    return Promise.reject(error);
});

export default {
    axios
}